<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Siteproduct_model extends My_Model
{    
    public function __construct()
    {
        parent::__construct();
        $this->model_name="站点产品";
        $this->tablename="sites_product";  
    }
        
    public function getpidbypage($siteid,$page)
    {
        $pagesize=500;
        $offset=$pagesize*($page-1);
        $where = " siteid={$siteid}";
        $query=$this->db->from($this->table_prefix.$this->tablename)->select("productid")->where($where)->limit($pagesize, $offset)->order_by("id asc")->get();
        $data=$query->result_array();    
        $rinfo=[];
        if($data)
        {
            foreach($data as $item)
            {
                $rinfo[]=intval($item["productid"]);
            }
        }
        return $rinfo;       
    }

    public function checkrids($siteid,$rids){
        $ids=implode(",",$rids);
        $where = " siteid={$siteid} and rid in ({$ids})";
        $query=$this->db->from($this->table_prefix.$this->tablename)->select("rid")->where($where)->get();
        $data=$query->result_array();    
        $rinfo=[];
        if($data)
        {
            foreach($data as $item)
            {
                $rinfo[]=intval($item["rid"]);
            }
        }
        return $rinfo;
    }

    public function getpage($siteid,$page,$pagesize=0,$key="",$sort=""){
    
        $where=["siteid"=>$siteid];
        if($pagesize>0)
        {
            $this->pagesize=$pagesize;
        }
        if($key)
        {
            $key=["title"=>$key];
        }
        if($sort)
        {
            $sort=$sort." desc";
        }
        else
        {
            $sort="sellcount desc,id desc";
        }
        return $this->get_items_by_page($where,$page,"backend/sites/product/index/".$siteid,$sort,[],$key);                
    }

    public function geturls($maxid,$siteid)
    {
        $where="siteid=".$siteid." and id>".$maxid;
        $query=$this->db->from($this->table_prefix.$this->tablename)->where($where)->limit(20)->order_by("id asc")->get();
        $data=$query->result_array();    
        return $data;
    }

    public function getcatproductcount($siteid,$rid)
    {
        $where=["siteid"=>$siteid,"cid"=>$rid];
        $count=$this->getcount($where);
        return $count;
    }


    public function getspecial()
    {
        $where=" siteid=27 and addtime between '2019-10-12 10:16:20','2019-10-12 12:22:53'";
        return $this->get_items_by_where($where);
    }

    public function getnosell($siteid)
    {
        $where= "siteid = {$siteid} and sellcount=0";
        return $this->get_items_by_where($where);
    }

    public function addsell($id,$num)
    {
        $this->auto_add("sellcount",["id"=>$id],$num);
    }

    public function getitemsfilter($filter)
    {
        return $this->get_items_by_where($filter);
    }
    
    
    public function getsitepro($siteid,$rid)
    {
        $where=["siteid"=>$siteid,"rid"=>$rid];
        return $this->get_item_by_where($where);
    }

    public function getproductcount($siteid)
    {
        $where=["siteid"=>$siteid];
        $count=$this->getcount($where);
        return $count;
    }


    public function getall(){
        $where=[];
        return $this->get_items_by_where($where);                
    }

    public function getitemkeyfilter($filter)
    {
        return $this->get_item_by_where($filter);
    }

    public function delitem($id)
    {
        $where=["id"=>$id];
        $this->delete($where);
        return true;
    }

    public function getitembyid($id){
        $where=array("id"=>$id);
  
        return $this->get_item_by_where($where);
    }

    public function create($data){
        if(!isset($data["addtime"])){
            $data["addtime"]=date("Y-m-d H:i:s");
        }
       
        return $this->add_item($data);       
    }

    public function updatelink($info,$siteid)
    {
        $newdata=["url"=>$info["url"]];
        $where="siteid=".$siteid." and rid=".$info["id"];
        return $this->update_item($where,$newdata);
    }


    public function update($id,$data){

        if(is_array($id))
        {
            $where="id in (".implode(",",$id).")";
           
        }else{
            $where=array("id"=>$id);
           
        }
        return $this->update_item($where,$data);

    }

   
}
